<%@ page language="java" import="java.util.*,com.demodis.domain.TblUser" pageEncoding="UTF-8"%>
<%
	String deterUrl = ((TblUser) request.getSession().getAttribute(
			"user")).getDeterUrl();
	String queryStatUrl = ((TblUser) request.getSession().getAttribute(
			"user")).getQueryStatUrl();
	String userFlag = ((TblUser) request.getSession().getAttribute(
			"user")).getUsername();
	String opBtnStr = "";
	String opFlagStr = "";
	if (userFlag.equalsIgnoreCase("admin")
			|| userFlag.equalsIgnoreCase("guest")) {
		opBtnStr = "{text : '增 加',iconCls : 'icon-add',handler : function() {moduls_deterGroup_addFun();}}, '-', {text : '删 除',iconCls : 'icon-remove',handler : function() {moduls_user_removeFun();}} , '-', ";
		opFlagStr = "'<img onclick=\"moduls_deterGroup_editFun(\\\'{0}\\\');\" src=\"{1}\"/>&nbsp;&nbsp;&nbsp;<img onclick=\"moduls_deterGroup_deleteFun(\\\'{2}\\\', \\\'{3}\\\');\" src=\"{4}\"/>', row.dgId, 'jslib/jquery-easyui-1.3.1/themes/icons/pencil.png', row.dgId, row.groupName + '-' + row.detersubjCont, 'jslib/jquery-easyui-1.3.1/themes/icons/cancel.png'";
	}
	//System.out.println(opFlagStr);
%>
<script type="text/javascript" charset="UTF-8">
var moduls_deterGroup_propertyGrid;
$(function() {
	moduls_deterGroup_doDeterModel();
});    
function moduls_deterGroup_doDeterModel() {
	moduls_deterGroup_propertyGrid = $('#moduls_deterGroup_propertyGrid').propertygrid({
		url : '${pageContext.request.contextPath}/deterGroupAction!query.action',
		showGroup : true,
		scrollbarSize : 0,
		fit : true,
		fitColumns : false,
		groupField : 'groupName',
		border : false,		
		toolbar : [{
			text : '展开',
			iconCls : 'icon-redo',
			handler : function() {
				var node = $('#moduls_deterGroup_propertyGrid').propertygrid('getSelected');
				if (node) {
					$('#moduls_deterGroup_propertyGrid').propertygrid('expandGroup', node.cid);
				} else {
					$('#moduls_deterGroup_propertyGrid').propertygrid('expandGroup');
				}
			}
		}, '-',{
			text : '折叠',
			iconCls : 'icon-undo',
			handler : function() {
				var node = $('#moduls_deterGroup_propertyGrid').propertygrid('getSelected');
				if (node) {
					$('#moduls_deterGroup_propertyGrid').propertygrid('collapseGroup', node.cid);
				} else {
					$('#moduls_deterGroup_propertyGrid').propertygrid('collapseGroup');
				}
			}
		}, '-',{
			text : '刷新',
			iconCls : 'icon-reload',
			handler : function() {
				$('#moduls_deterGroup_propertyGrid').propertygrid('reload');
			}
		}, '-',{
			text : '测  评',
			iconCls : 'icon-ok',
			handler : function() {
				moduls_deterGroup_doDeter();
			}
		}, '-', <%=opBtnStr%>{
			text : '查看模式',
			iconCls : 'icon-search',
			handler : function() {
				var deterUrl = '<%=deterUrl%>';
				if(deterUrl=='null') {
					$.messager.show({
						title : '提示',
						msg : '权限不足!'
					});
				} else {
					moduls_deterGroup_queryDeterModel(deterUrl);
				}
			}
		}, '-', {
			text : '参评人数统计',
			iconCls : 'icon-sum',
			handler : function() {
				var queryStatUrl = '<%=queryStatUrl%>';
				if(queryStatUrl=='null') {
					$.messager.show({
						title : '提示',
						msg : '权限不足!'
					});
				} else {
					moduls_deterGroup_deterStat(queryStatUrl);
				}
			}
		} ],
		columns : [ [ {
			title : '组测评id',
			field : 'dgId',
			width : 150,
			hidden : true
		}, {
			title : '被测评组id',
			field : 'groupId',
			width : 150,
			hidden : true
		}, {
			title : '测评项目名称',
			field : 'detersubjCont',
			width : 150
		}, {
			title : '测评选项',
			field : 'deterRes',
			width : 250,
			formatter : function(value, row, index) {
				var str = '<select dgId="'+row.dgId+'"><option value="-1"></option><option value="15">15</option><option value="12">12</option>'
						  + '<option value="9">9</option><option value="6">6</option><option value="3">3</option>'
						  + '<option value="0">0</option></select>';
				return str;
			}
		}, {
			field : 'action',
			title : '动作',
			width : 100,
			formatter : function(value, row, index) {
				if (row.id == '0') {
					return '系统角色';
				} else {
				    return formatString(<%=opFlagStr%>);
					//return formatString('<img onclick="moduls_deterGroup_editFun(\'{0}\');" src="{1}"/>&nbsp;&nbsp;&nbsp;<img onclick="moduls_deterGroup_deleteFun(\'{2}\', \'{3}\');" src="{4}"/>', row.dgId, 'jslib/jquery-easyui-1.3.1/themes/icons/pencil.png', row.dgId, row.group + '-' + row.detersubjCont, 'jslib/jquery-easyui-1.3.1/themes/icons/cancel.png');
				}
			}
		} ] ]
	});
}
var drawPieColor = [ "#579575", "#d8b83f", "#ff5800", "#0085cc" ];
var drawPie = function(divName, pieData, pieName, peiColor) {
	$.jqplot(divName, [ pieData ], {
		grid : {
			drawBorder : false,
			drawGridlines : false,
			background : '#ffffff',
			shadow : false
		},
		title : pieName,
		seriesColors : peiColor,
		series : [ {
			renderer : $.jqplot.PieRenderer,
			rendererOptions : {
				diameter : 120,//饼图的直径
				sliceMargin : 0,//饼的每个部分之间的距离
				showDataLabels : true,
				dataLabelNudge : 15,
				shadow : true//为饼的每个部分的边框设置阴影，以突出其立体效果
			}
		} ],
		legend : {
			show : true,
			location : 'w',
			xoffset : 1
		}
	});
};

var pieCount = 0;
var pieObjs = [];
function moduls_deterGroup_queryDeterModel(deterUrl) {
	moduls_deterGroup_propertyGrid = $('#moduls_deterGroup_propertyGrid').propertygrid({
		url : '${pageContext.request.contextPath}'+deterUrl,
		showGroup : true,
		scrollbarSize : 0,
		fit : true,
		fitColumns : false,
		groupField : 'groupName',
		border : false,
		toolbar : [ {
			text : '展开',
			iconCls : 'icon-redo',
			handler : function() {
				var node = $('#moduls_deterGroup_propertyGrid').propertygrid('getSelected');
				if (node) {
					$('#moduls_deterGroup_propertyGrid').propertygrid('expandGroup', node.cid);
				} else {
					$('#moduls_deterGroup_propertyGrid').propertygrid('expandGroup');
				}
			}
		}, '-', {
			text : '折叠',
			iconCls : 'icon-undo',
			handler : function() {
				var node = $('#moduls_deterGroup_propertyGrid').propertygrid('getSelected');
				if (node) {
					$('#moduls_deterGroup_propertyGrid').propertygrid('collapseGroup', node.cid);
				} else {
					$('#moduls_deterGroup_propertyGrid').propertygrid('collapseGroup');
				}
			}
		}, '-', {
			text : '返回',
			iconCls : 'icon-reload',
			handler : function() {
				moduls_deterGroup_doDeterModel();
			}
		} ],
		columns : [ [{
			title : '测评选项',
			field : 'detersubjCont',
			width : 500,
			formatter : function(value, row, index) {
				var pieObj = new Object();
				pieObj.divName = 'pie' + pieCount;
				pieObj.pieData = [];
				var zf = 0;
				var option = value.split(',');
				var value = row.deterStr.split(',');
				for ( var i = 0; i < option.length; i++) {
					var dataArr = []; 
					dataArr.push(option[i] + '：' + (value[i] / row.deterNum).toFixed(2));
					dataArr.push(1 * value[i]);
					zf = zf + 1 * value[i];
					pieObj.pieData.push(dataArr);
				}
				pieObj.pieName = '总平均分：' + (zf / row.deterNum / 2).toFixed(2);
				var str = '<table><tr>';
					str += '<td><div id="pie'+pieCount+'" style="height:190px; width:280px;background-color: white;border-style:none;"></div></td>';
				str += '</tr></table>';
				//console.info(str);
				pieObjs.push(pieObj);
				pieCount++;
				return str;
			}
		} ] ],
		onLoadSuccess : function() {
			//console.info(pieObjs);
			for(var i = 0; i < pieObjs.length; i++) {
				drawPie(pieObjs[i].divName, eval(pieObjs[i].pieData), pieObjs[i].pieName, drawPieColor);
			}
			pieCount = 0;
			pieObjs = [];
		}
	});
}

function moduls_deterGroup_deterStat(url) {
	moduls_deterGroup_propertyGrid = $('#moduls_deterGroup_propertyGrid').propertygrid({
		url : '${pageContext.request.contextPath}' + url,
		fit : true,
		border : false,
		pagination : true,
		checkOnSelect : false,
		selectOnCheck : false,
		groupField : 'groupName',
		idField : 'userId',
		nowrap : false,
		columns : [ [ {
			title : '编号',
			field : 'userId',
			width : 150,
			checkbox : true,
			hidden : true
		}, {
			title : '工号',
			field : 'username',
			width : 80
		}, {
			title : '用户姓名',
			field : 'userRealName',
			width : 80
		}, {
			title : '所属部门',
			field : 'groupId',
			width : 150,
			formatter : function(value, row, index) {
				if (value == 0)
					return '';
				return row.groupName;
			}
		}, {
			title : '拥有角色',
			field : 'roleId',
			width : 130,
			formatter : function(value, row, index) {
				if (value == 0)
					return '';
				return row.roleName;
			}
		}, {
			title : '测评状态',
			field : 'detered',
			width : 100,
			formatter : function(value, row, index) {
				if (value == 0)
					return '';
				return '<font color="red">已测评</font>';
			}
		}] ],
		toolbar : [{
			text : '展开',
			iconCls : 'icon-redo',
			handler : function() {
				var node = $('#moduls_deterGroup_propertyGrid').propertygrid('getSelected');
				if (node) {
					$('#moduls_deterGroup_propertyGrid').propertygrid('expandGroup', node.cid);
				} else {
					$('#moduls_deterGroup_propertyGrid').propertygrid('expandGroup');
				}
			}
		}, '-', {
			text : '折叠',
			iconCls : 'icon-undo',
			handler : function() {
				var node = $('#moduls_deterGroup_propertyGrid').propertygrid('getSelected');
				if (node) {
					$('#moduls_deterGroup_propertyGrid').propertygrid('collapseGroup', node.cid);
				} else {
					$('#moduls_deterGroup_propertyGrid').propertygrid('collapseGroup');
				}
			}
		}, '-', {
			text : '返回',
			iconCls : 'icon-undo',
			handler : function() {
				moduls_deterGroup_doDeterModel();
			}
		} ]
	});
}

function moduls_deterGroup_addFun() {
	moduls_deterGroup_propertyGrid.propertygrid('uncheckAll').propertygrid('unselectAll').propertygrid('clearSelections');
	$('<div/>').dialog({
		href : '${pageContext.request.contextPath}/moduls/fun/addDeterGroup.jsp',
		width : 400,
		height : 200,
		modal : true,
		title : '添加组测评对象',
		buttons : [ {
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#moduls_fun_addDeterGroup_addForm').form('submit', {
					url : '${pageContext.request.contextPath}/deterGroupAction!add.action',
					success : function(result) {
						var r = $.parseJSON(result);
						if (r.success) {
							moduls_deterGroup_propertyGrid.propertygrid('load');
							d.dialog('destroy');
						}
						$.messager.show({
							title : '提示',
							msg : r.msg
						});
					}
				});
			}
		}, {
			text : '取消',
			iconCls : 'icon-remove',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		}
	});
}

function moduls_deterGroup_editFun(id) {
	moduls_deterGroup_propertyGrid.propertygrid('uncheckAll').propertygrid('unselectAll').propertygrid('clearSelections');
	var index = moduls_deterGroup_propertyGrid.propertygrid('getRowIndex', id);
	moduls_deterGroup_propertyGrid.propertygrid('selectRow', index);
	$('<div/>').dialog({
		href : '${pageContext.request.contextPath}/moduls/fun/editDeterGroup.jsp',
		width : 400,
		height : 200,
		modal : true,
		title : '测评对象编辑',
		buttons : [ {
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#moduls_fun_editDeterGroup_editForm').form('submit', {
					url : '${pageContext.request.contextPath}/deterGroupAction!modify.action',
					success : function(result) {
						try {
							var r = $.parseJSON(result);
							if (r.success) {
								moduls_deterGroup_propertyGrid.propertygrid('load');
								d.dialog('destroy');
							}
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						} catch (e) {
							$.messager.alert('提示', result);
						}
					}
				});
			}
		}, {
			text : '取消',
			iconCls : 'icon-remove',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		},
		onLoad : function() {
			var rows = moduls_deterGroup_propertyGrid.propertygrid('getSelected');
			$('#moduls_fun_editDeterGroup_editForm').form('load', rows);
		}
	});
}

function moduls_deterGroup_deleteFun(node, text) {
	if (node) {
		$.messager.confirm('询问', '您确定要删除【' + text + '】？', function(b) {
			if (b) {
				$.ajax({
					url : '${pageContext.request.contextPath}/deterGroupAction!delete.action',
					data : {
						ids : node
					},
					cache : false,
					dataType : 'JSON',
					success : function(r) {
						if (r.success) {
							moduls_deterGroup_propertyGrid.propertygrid('load');
						}
						$.messager.show({
							msg : r.msg,
							title : '提示'
						});
					}
				});
			}
		});
	}
}

function moduls_deterGroup_doDeter() {
	var deterRes = [];
	if ($("select[value=-1]").length == 0) {
		$("select[dgid!=0]").each(function(i, value) {
			var obj = {};
			obj.dgId = $(value).attr('dgid');
			obj.value = value.value;
			deterRes.push(obj);
		});
		//console.info($.toJSON(deterRes));
		$.ajax({
			type : 'POST',
			url : '${pageContext.request.contextPath}/deterGroupAction!doDeter.action',
			data : {
				deterStr : $.toJSON(deterRes)
			},
			cache : false,
			dataType : 'JSON',
			success : function(r) {
				if (r.success) {
					moduls_deterGroup_propertyGrid.propertygrid('load');
				}
				$.messager.show({
					msg : r.msg,
					title : '提示'
				});
			}
		});
	} else {
		$.messager.show({
			msg : '请测评所有项目!',
			title : '提示'
		});
	}
}
</script>
<table id="moduls_deterGroup_propertyGrid"></table>
